/**
  ******************************************************************************
  * @file           : tm1638.h
  * @brief          : Header for usart.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TM1638_H__
#define __TM1638_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "xl32f003xx_ll_Start_Kit.h"


/* Private includes ----------------------------------------------------------*/

/* Exported types ------------------------------------------------------------*/


typedef struct tm1638_struct{
  uint8_t    CLK_GPIOx;        //SCL的端口
  uint32_t   CLK_PIN_x;       //SCL的引脚
  uint8_t    DIO_GPIOx;        //SDA的端口
  uint32_t   DIO_PIN_x;        //SDA的引脚
  uint8_t    STB_GPIOx;        //SDA的端口
  uint32_t   STB_PIN_x;        //SDA的引脚
}TM1638_TypeDef;

extern TM1638_TypeDef tm1638;;
extern uint8_t DispTab[4];
extern uint8_t KeyValue[4];  //保存按键值
extern uint8_t Update_disp_flg;

#define TM1638_STB(t,x)                  GPIO_Set(t->STB_GPIOx,t->STB_PIN_x,x);
#define TM1638_CLK(t,x)                  GPIO_Set(t->CLK_GPIOx,t->CLK_PIN_x,x);
#define TM1638_DIO_IN(t)                 GPIO_Init(t->DIO_GPIOx,t->DIO_PIN_x,GPIO_ModeIN_PU);
#define TM1638_DIO_OUT(t)                GPIO_Init(t->DIO_GPIOx,t->DIO_PIN_x,GPIO_ModeOut_PP_5mA);             
#define TM1638_DIO_DataOUT(t,x)          GPIO_Set(t->DIO_GPIOx,t->DIO_PIN_x,x);
#define TM1638_DIO_DataIN(t)             GPIO_Read_In(t->DIO_GPIOx,t->DIO_PIN_x)


//地址命令设置
#define TM1638_GRID1  0xc0
#define TM1638_GRID2  0xc1
#define TM1638_GRID3  0xc2
#define TM1638_GRID4  0xc3
#define TM1638_GRID5  0xc4
#define TM1638_GRID6  0xc5
#define TM1638_GRID7  0xc6
#define TM1638_GRID8  0xc7

#define TM1638_GRID9  0xc8
#define TM1638_GRID10 0xc9
#define TM1638_GRID11 0xca
#define TM1638_GRID12 0xcb
#define TM1638_GRID13 0xcc
#define TM1638_GRID14 0xcd
#define TM1638_GRID15 0xce
#define TM1638_GRID16 0xcf


//数据命令设置
/*
#define 0x40   //写数据到显示寄存器
#define 0x42   //读键扫数据
#define 0x40   //自动地址增加
#define 0x44   //固定地址
#define 0x40   //普通模式
#define 0x48   //测试模式
*/

//显示
/*
#define 0x80   //显示关
#define 0x88   //显示开
*/




//dig
#define  GRID(x)  (x-1)*2
#define SEG(x)  1<<(x-1)
  

#define SysBlueLed(s)        Digital_led(8,1,s)
#define NetRedLed(s)         Digital_led(8,2,s)
#define PwrBlueLed(s)        Digital_led(8,3,s)
#define PwrRedLed(s)         Digital_led(8,4,s)
#define RS485BlueLed(s)      Digital_led(8,5,s)
#define GSMRedLed(s)         Digital_led(8,6,s)
#define NetLinkBlueLed(s)    Digital_led(8,7,s)
#define reserved1(s)         Digital_led(8,8,s)

#define ExchangerBlueLed(s)  Digital_led(9,1,s)
#define reserved2(s)         Digital_led(9,2,s)
#define Camera1BlueLed(s)    Digital_led(10,1,s)
#define reserved3(s)         Digital_led(10,2,s)

#define Camera2BlueLed(s)    Digital_led(10,3,s)
#define reserved4(s)         Digital_led(10,4,s)
#define Camera3BlueLed(s)    Digital_led(10,5,s)
#define reserved5(s)         Digital_led(10,6,s)
#define Camera4BlueLed(s)    Digital_led(10,7,s)
#define reserved6(s)         Digital_led(10,8,s)
#define reserved7(s)         Digital_led(11,1,s)
#define reserved8(s)         Digital_led(11,2,s)


void Read_key(TM1638_TypeDef *TM1638,uint8_t *KeyValue);
void TM1638_Init(TM1638_TypeDef *TM1638);
void dispclear(TM1638_TypeDef *TM1638,uint8_t n,uint8_t m);
void Flicker(TM1638_TypeDef *TM1638,uint8_t n);
void display_led(TM1638_TypeDef *TM1638,uint8_t dig,uint8_t dat);
void display(uint8_t dig,uint8_t dat);
void Digital_led(TM1638_TypeDef *TM1638,uint8_t dig,uint16_t seg,uint8_t onoff);
void UpdateDisp(TM1638_TypeDef *TM1638);
void Write_DATA(TM1638_TypeDef *TM1638,uint8_t add,uint8_t DATA);
uint8_t TM1638_Read(TM1638_TypeDef *TM1638);
void TM1638_Write(TM1638_TypeDef *TM1638,uint8_t DATA);

#endif

